
package com.probiz.estore.common.util;

import java.util.Date;
import java.util.List;

import cn.emay.sdk.client.api.Client;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.system.SmsQueue;
import com.probiz.estore.core.util.I18nUtil;
import com.probiz.estore.system.service.SmsQueueManager;

public class SmsUtil {

	private SmsQueueManager	smsQueueManager	= null;

	public void setSmsQueueManager(SmsQueueManager smsq) {
		smsQueueManager = smsq;
	}

	private static SmsUtil		instance	= new SmsUtil();
	private static String		smsKey		= "smsKey";

	public static SmsUtil getInstance() {
		return instance;
	}

	private SmsUtil() {
	}

	/**
	 * 短信发送：每次发送最早的前5条
	 * 
	 * */
	public void sendMuilSms() { 
		ConfigUtil config = ConfigUtil.getInstance();
		Integer batchCount = config.getSmsBatchSendCount();
		Integer maxNum = config.getSmsMaxSendTimes();
		List<SmsQueue> smsList = smsQueueManager.getSmsQueueToSend(batchCount==null?10:batchCount, maxNum==null?3:maxNum);//每次取最早的前5条数据
		Client sdkClient = null;
		try {
			sdkClient = new Client(config.getSerialNo(), config.getSerialNoPassWord());
		} catch (Exception e) {
			e.printStackTrace(); 
		}
		if (smsList != null && smsList.size() > 0) {
			Date now = new Date();
			for (SmsQueue sq : smsList) {				
				
				if (config.isOpenMsgSend()) {
					String ph = sq.getPhoneNumbers();
					String[] mobiles = ph.split(",");
					int i = sdkClient.sendSMS(mobiles, sq.getSmsContent(), 1);
					if (i == 0) {
						sq.setStatus(SmsQueue.SMS_ISSEND);
					} else {
						sq.setStatus(SmsQueue.SMS_NOSEND);
					}
					sq.setSendTime(now);
					String status = I18nUtil.getInstance().getMessage("msgsend.status_" + i);
					sq.setSendStatus(status);
					sq.setSendMaxNum(sq.getSendMaxNum()+1);
					smsQueueManager.save(sq);
				}
			}
			
		}
		
	}

	public SmsQueue sendMsg(SmsQueue sq, String[] mobile, String shortMessageContent) {
		Client sdkClient = null;
		try {
			ConfigUtil config = ConfigUtil.getInstance();
			sdkClient = new Client(config.getSerialNo(), config.getSerialNoPassWord());
		} catch (Exception e) {
			e.printStackTrace();
		}
		int i = sdkClient.sendSMS(mobile, shortMessageContent, 1);
		if (i == 0) {
			sq.setStatus(SmsQueue.SMS_ISSEND);
		} else {
			sq.setStatus(SmsQueue.SMS_NOSEND);
		}
		Date now = new Date();
		sq.setSendTime(now);
		String status = I18nUtil.getInstance().getMessage("msgsend.status_" + i);
		sq.setSendStatus(status);
		smsQueueManager.save(sq);
		
		return sq;
	}

	// 注册成功的账号要把注销之后才能换其它的账号
	public int register() {
		int result = 555;
		try {
			ConfigUtil config = ConfigUtil.getInstance();
			Client sdkClient = new Client(config.getSerialNo(), config.getSerialNoPassWord());
			result = sdkClient.registEx(config.getSerialNoPassWord());
		} catch (Exception e) {
			e.printStackTrace();
			result = 555;
		}
		return result;
	}

	// 注消
	public int logout() {
		int result = 555;
		try {
			ConfigUtil config = ConfigUtil.getInstance();
			Client sdkClient = new Client(config.getSerialNo(), config.getSerialNoPassWord());
			result = sdkClient.logout();
		} catch (Exception e) {
			e.printStackTrace();
			result = 555;
		}
		return result;
	}

	public double getBalance() {
		double balance = 0.0d;
		try {
			ConfigUtil config = ConfigUtil.getInstance();
			Client sdkClient = new Client(config.getSerialNo(), config.getSerialNoPassWord());
			balance = sdkClient.getBalance();
			return balance;
		} catch (Exception e) {
			e.printStackTrace();
			balance = -1;
		}
		return balance;
	}

}
